<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
import { userAnalysis } from '@/api/home'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      userAnalysis().then(res => {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(res.data)
      }).catch(() => {
        this.loading = false
      })
    },
    setOptions({ dates, ipNumbers, loginNumbers, operationNumbers } = {}) {
      this.chart.setOption({
        xAxis: {
          data: dates,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [
          {
            name: '访问IP数',
            minInterval: 1,
            position: 'left'
          },
          {
            name: '操作总数',
            minInterval: 1,
            position: 'right',
            inverse: true
          }
        ],
        legend: {
          data: ['访问IP数', '登录人数', '操作总数']
        },
        series: [
          {
            name: '访问IP数',
            itemStyle: {
              color: '#13C2C2',
              lineStyle: {
                color: '#13C2C2',
                width: 2
              }
            },
            smooth: true,
            type: 'line',
            data: ipNumbers,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '登录人数',
            smooth: true,
            type: 'line',
            itemStyle: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              }
            },
            data: loginNumbers,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '操作总数',
            smooth: true,
            type: 'line',
            itemStyle: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            },
            data: operationNumbers,
            yAxisIndex: 1,
            animationDuration: 2800,
            animationEasing: 'quarticOut'
          }]
      })
    }
  }
}
</script>
